#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow)
{

    QTextCodec::setCodecForTr(QTextCodec::codecForName ("UTF-8"));
    currentDir = QDir("/home/lmviiitest/Projekty/");
    ui->setupUi(this);
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    QStringList labels;
    labels << tr("Directory") << tr("File Name") << tr("Line") << tr("Size");
    ui->tableWidget->setHorizontalHeaderLabels(labels);
    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->hide();
    ui->tableWidget->setShowGrid(false);
    //ui->tableWidget->columnWidth(1);
    ui->tabWidget->removeTab(6);
    ui->tabWidget->removeTab(5);
    ui->tabWidget->removeTab(4);
    ui->tabWidget->removeTab(3);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(1);




    connect(ui->import_b, SIGNAL(clicked()), this, SLOT(openfiles_v()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openfiles_v() //Import
{
    ImportDialog Idial(this, Baza, currentDir);
    if (Idial.exec())
    {
        ui->textEdit->append(Idial.log);
        ui->comboBox->addItem(Idial.nazwa_projektu);
        this->dialli = Idial.dialli;
        this->Baza = Idial.Baza;
        this->openfile = Idial.openfile;
    }

}

void MainWindow::on_pushButton_4_clicked() // Expotr
{
    qDebug() << "file.fileName()";
    CSkrypty Import;
    QString kod;
    for (QStringList::Iterator it = openfile.begin(); it != openfile.end(); ++it)
    {
    QFile file;
    file.setFileName(*it);
    qDebug() << file.fileName();
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    Import.parsuj(file, Baza, kod, 2);
    file.close();
    }

}

void MainWindow::on_pushButton_2_clicked() //Wczytaj
{
    QFileInfo fi;


    for (int i = 0; (i<(dialli)); i++)
    {
        fi.setFile(Baza[i].sciezka);
        ui->tableWidget->insertRow(i);
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(currentDir.relativeFilePath(fi.path())));
        ui->tableWidget->setItem(i, 0, newItem);
        QTableWidgetItem *newItem2 = new QTableWidgetItem(tr("%1").arg(fi.fileName()));
        ui->tableWidget->setItem(i, 1, newItem2);
        QTableWidgetItem *newItem3 = new QTableWidgetItem(tr("%1").arg(QString::number(Baza[i].nr_wiersza)));
        ui->tableWidget->setItem(i, 2, newItem3);
        QTableWidgetItem *newItem4 = new QTableWidgetItem(tr("%1").arg(Baza[i].tresc));
        ui->tableWidget->setItem(i, 3, newItem4);
        ui->tabWidget->insertTab(1, ui->tab_3, "Dialogi");
        ui->tabWidget->setCurrentIndex(1);
    }

    connect(ui->tableWidget, SIGNAL(cellActivated(int,int)),
            this, SLOT(editItem(int,int)));
}
void MainWindow::editItem(int row, int /* column */)
{
    ui->tabWidget->insertTab(ui->tabWidget->count(), ui->tab_6, "Edycja");
    ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
    //qDebug()<<row;
    writeToForm(row);

    //QTableWidgetItem *item = filesTable->item(row, 0);

    //QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));
}
void MainWindow::writeToForm(int id)
{
    QFileInfo fi;
    fi.setFile(Baza[id].sciezka);
    ui->la_filename->setText(tr("%1").arg(fi.fileName()));
    ui->le_lang1->setText(Baza[id].tresc);
}
